<?php
include_once ('allowdenyplugin.php');

class iFramework
{
	var $config_section;
	var $plugins;
	var $plugins_hierarhy;
	var $plugins_folder;
	var $plugin_export_function_name;
	var $smarty;
	var $default_plugin;
	var $db;
	var $allowdenyclass;
	var $permission = 'permission';
	
	function iFramework($config_section = "", $arg = array(
							"plugins_folder" => "",
							"plugin_export_function_name" => "Export",
							"default_plugin" => "",
							"template_dir" => "",
							"compile_dir" => "",
							"config_dir" => "",
							"cache_dir" => "",
							))
	{
		$this->allowdenyclass =& new allowdenyacces();
		$this->config_section = "" == $config_section ? "framework" : $config_section;
		$this->plugins = null;
		$this->db = null;
		$this->plugin_export_function_name = $arg["plugin_export_function_name"];

		if("" == $arg['permission'])
		{
			$this->permission = $arg['permission'];
		}

		
		if("" == $arg["plugins_folder"])
		{
			$this->plugins_folder = CORE_PLUGINS_FOLDER;
		}
		else
		{
			$this->plugins_folder = $arg["plugins_folder"];
		}
		$this->default_plugin = $arg["default_plugin"];
		$this->smarty = createSmartyInstance($arg);
	}

	function initialize()
	{
		if("" == $this->default_plugin)
		{
			$core = getCoreInstance();
			$this->default_plugin = $core->config[$this->config_section]["default_plugin"];
		}
		$this->locatePlugins();
		$this->initPlugins();
	}

	function shutdown()
	{
		$this->shutdownPlugins();
		unset($this->plugins);
		$this->plugins = null;
	}
	
	function addPluginHierarhy(&$plugin, $path)
	{
		if(null != $plugin)
		{
			$pname = $plugin->getPluginTag();
			$ppath = ("" == $path ? "" : "{$path},").$pname;
			$this->plugins_hierarhy[$pname] = $ppath;
			if( count($plugin->childs) )
			{
				foreach ($plugin->childs as $p)
				{
					$this->addPluginHierarhy(&$plugin->childs[$p->getPluginTag()], $ppath);
				}
			}
		}
	}
	
	
	function locatePlugins()
	{
		$plg = array();
		$core = getCoreInstance();
		
		if ($handle = opendir($core->config["file_root"] . $this->plugins_folder))
		{
			// This is the correct way to loop over the directory.
			while (false !== ($file = readdir($handle)))
			{
				if((TRUE == is_dir($core->config["file_root"] . $this->plugins_folder . "/" . $file))
				&& (".." != $file)
				&& ("." != $file)
				&& ($file[0]!='.')
				)
				{
					if(!file_exists($core->config["file_root"] . $this->plugins_folder . "/" . $file . "/" . 'disabled')){
						if(0 < $_SESSION[$this->permission] or !isset($_SESSION[$this->permission])){
							if($this->allowdenyclass->allowdeny($_SESSION[$this->permission],$file)){
								$plg[] = $file;
							}
						}
					} // check if plugin is disabled for all (exists file named 'disabled'))
				}
			}
			closedir($handle);
		} // end if
		// -- Add plugins to the pool
		foreach ($plg as $tag)
		{
			//$this->plugins[] = $p;
			$file = $core->config["file_root"] . $this->plugins_folder . "/" . $tag . "/" . $tag. ".php";
			if(file_exists($file) && is_readable($file)){
    			 include_once($file);
    			 $obj = null;
    			 eval("\$obj = {$tag}_".$this->plugin_export_function_name."();");
    			 $tag = $obj->getPluginTag();
    			 $this->plugins[$tag] = $obj;
    			 $this->addPluginHierarhy(&$obj, "");
			}
		}
	}
	
	function initPlugins()
	{
		if( null == $this->plugins)
		{
			return;
		}
		foreach($this->plugins as $tag => $p)
		{
			$p->initialize();
		}

	}
	
	function shutdownPlugins()
	{
		if( null == $this->plugins)
		{
			return;
		}

		foreach ($this->plugins as $p)
		{
			$p->shutdown();
		}
	}
	
	function doAction($params = null)
	{
		return true;
	}
	
	function render($params = null)
	{
		return "Dummy interface";
	}

};

?>